home *** CD-ROM | disk | FTP | other *** search
- Path: dinews.epfl.ch!usenet
- From: Bernhard Ruch <Bernhard.Ruch@studi.epfl.ch>
- Newsgroups: comp.lang.c++,gnu.g++.help
- Subject: Re: Template function only for built-in types
- Date: Mon, 25 Mar 1996 17:25:18 +0100
- Organization: Ecole Polytechnique Federale de Lausanne
- Message-ID: <3156C8EE.41C6@studi.epfl.ch>
- References: <3151BA38.41C6@studi.epfl.ch> <4isjii$fvb@news.aimnet.com>
- NNTP-Posting-Host: didec8.epfl.ch
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; OSF1 V3.2 alpha)
-
- Ramnivas Laddad wrote:
- >
- > No. Problem is not "no operator in istream for class A". And you don't
- > need to modify istream for each such class.
- >
- > What you need is a function,
- > istream& operator >> (istream& rInStream, A& a)
- > {
- > // rInStream >> to A's members etc.
- > // ...
- > return rInStream;
- > }
- >
- > You need to have one such function for each of your class as how to
- > associate stream input to class attribute is a class specific matter.
- >
- > BTW, looks like you have typedefined istream to InSream. Am I right?
-
- No. Perhaps I have to clarify exactly, what my problem is:
-
- I want to write two new classes, InStream and OutStream. These two classes
- should contain the operators >> (and <<, resp.) for the built-in types, i.e.
- I should be able to write:
-
- InStream InStr;
- OutStream OutStr;
- char ch;
- int integ;
- float flo;
- ...
-
- InStr >> ch;
- OutStr << ch;
- InStr >> integ;
- OutStr << integ;
- InStr >> flo;
- OutStr << flo;
- ...
-
- But I don't want to be able to write all other types than the built-in types,
- especially I don't want to write:
-
- InStr >> A;
-
- The only other class that I want to be able to read or write is the class Object,
- but that's another problem.
-
- The two classes, InStream and OutStream, contain an object of type ifstream, (ofstream resp.).
- Now, in order to define the two operators >> and << for all built-in types, I wrote
- the two template functions:
-
- template <class T> inline OutStream &operator << (OutStream &OutStr, const T t);
- template <class T> inline InStream &operator >> (InStream &InStr, T t);
-
- Inside these template functions, I use the operators >> and << of the classes ifstream and
- ofstream, which should be defined for the built-in types.
-
- Unfortunately, the compiler tries to define those two operators not only for the built-in
- types, but also for my other classes, A and B, for which the corresponding operators of the
- classes istream and ostream do not exist. Therefore, I get the error:
-
- "No match for 'operator >> (class ifstream, class A)'"
-
- What I am looking for now is a way to define the operators >> and << for the build-in types
- only, and not for all other classes as well.
-
- Bernhard
-